﻿using System;
using System.Text;
using System.Windows.Forms;
using SDT.CodeSystem;
using SDT.CrawlSystem.Tools.Data;

namespace SDT.CrawlSystem.Tools
{
    public partial class CodesGen : Form
    {
        public CodesGen()
        {
            InitializeComponent();
        }

        private void insertcitiesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Repository.Insert(CityCodes.Codes.CN, null, CityCodes.Names[(int)CityCodes.Codes.CN], 1);
            Repository.Insert(CityCodes.Codes.MB, CityCodes.Codes.CN, CityCodes.Names[(int)CityCodes.Codes.MB], 2);
            Repository.Insert(CityCodes.Codes.MT, CityCodes.Codes.CN, CityCodes.Names[(int)CityCodes.Codes.MT], 3);
            Repository.Insert(CityCodes.Codes.MN, CityCodes.Codes.CN, CityCodes.Names[(int)CityCodes.Codes.MN], 4);

            foreach (var n in CityCodes.NorthenCities)
                Repository.Insert(CityCodes.FromString(n), CityCodes.Codes.MB, CityCodes.Names[(int)CityCodes.FromString(n)], 10);

            foreach (var n in CityCodes.CentralCities)
                Repository.Insert(CityCodes.FromString(n), CityCodes.Codes.MT, CityCodes.Names[(int)CityCodes.FromString(n)], 10);

            foreach (var n in CityCodes.SouthernCities)
                Repository.Insert(CityCodes.FromString(n), CityCodes.Codes.MN, CityCodes.Names[(int)CityCodes.FromString(n)], 10);
        }

        private void insertdistrictsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            foreach (var item in CityCodes.CityDistTable)
                for (int i = 0; i < item[2].Length; i++)
                    Repository.Insert(CityCodes.FromString(item[1][0]), item[3][i], item[2][i]);
        }

        private void CodesGen_Load(object sender, EventArgs e)
        {
            for (var c = CityCodes.Codes.CN; c <= CityCodes.Codes.VL; c++)
                LeftListBox.Items.Add(CityCodes.ToString(c));
        }

        private void LeftListBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (LeftListBox.SelectedItem != null)
            {
                RightListBox.Items.Clear();
                var city = LeftListBox.SelectedItem as string;

                foreach (var item in CityCodes.CityDistTable)
                {
                    if (item[1][0] == city)
                    {
                        for (int i = 0; i < item[2].Length; i++)
                        {
                            RightListBox.Items.Add(item[2][i] + " - " + item[3][i]);
                        }
                        break;
                    }
                }
            }
        }

        private void genShortNameToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var input = txtIn.Text.Trim();

            var col = input.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

            var b = new StringBuilder();
            foreach (var _city in col)
            {
                var city = _city.Trim(new char[] { '\'' });
                var words = city.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                b.Append('"');
                foreach (var w in words)
                {
                    b.Append(w.ToUpper()[0]);
                }
                b.Append("\", ");
            }

            txtOut.Text = b.ToString();
        }
    }
}